/***
 * yakuPaging.js pager
 *
 * @sinde 2010-12-03
 * @author jsyang <yakuyaku@gmail.com>
 * @version 1.3
 */

;(function($){

    $.fn.paging = function(opts)
    {
    var options = $.extend($.fn.paging.defaults, opts);
    var current_page = options.current_page;
    var total_record = options.total_record;
    var total_page   = Math.ceil(options.total_record / options.items_per_page);
    var start_number;
    var last_start_page;

    startNum();
    lastNum();

    return $(this).each(function(){

    var $container = $(this);

    var appendItem = function(page_id, appendopts, addTag, direction )
    {
    appendopts = $.extend({text:page_id, classes:""}, appendopts||{});
    var lnk = "",txt = appendopts.text;
    if(page_id == current_page) {
    lnk = $("<"+ options.selected_tag + " class='" + options.selected_class + "'>"+ txt +"</"+ options.selected_tag +">");
    }else {
    if( direction == "left" ) {
    txt =  addTag + txt;
    } else {
    txt =  txt + addTag;
    }
    lnk = $("<a>"+ txt + "&nbsp;</a>").attr('href', options.link_to.replace(/__id__/,page_id));
    }

    if(appendopts.classes) {
    lnk.addClass(appendopts.classes);
    }

    $container.append(lnk);
    }

    var makePaging = function() {

    $container.empty();

    var interval = getInterval();

    if ( current_page > 1 && options.first_last_show ) {
    appendItem(1,{text:options.first_text, classes:options.first_class}, options.addTag, "first");
    }

    if(options.prev_text && options.prev_show_always){
    if(start_number > 1 ) {
    appendItem(parseInt(interval[0])-1,{text:options.prev_text, classes:options.prev_class}, options.addTag, "left");
    }
    }

    for(var i=interval[0]; i<=interval[1]; i++) {
    appendItem(i,{classes: options.link_class},"","");
    }

    if(options.next_text && options.next_show_always){
    if (current_page < last_start_page){
    appendItem(parseInt(interval[1])+1,{text:options.next_text, classes:options.next_class}, options.addTag, "right");
    }
    }

    if ( current_page < total_page && options.first_last_show ) {
    appendItem(total_page,{text:options.last_text, classes:options.last_class}, options.addTag, "last");
    }

    if ( $.isFunction(options.link_click) ) {
    $container.find('a.' + options.link_class).unbind('click');
    $container.find('a.' + options.link_class).click(function(e){
    e.preventDefault();
    var str = options.link_to.replace(/__id__/,'').replace("?","");
    var pattern = new RegExp(str + "[0-9]+");
    var pageNumber = this.href.match(pattern);
    if ( pageNumber ) {
    pageNumber = pageNumber.toString().replace(str, "");
    current_page = pageNumber;
    startNum();
    lastNum();
    options.link_click.apply(this,[this.href,pageNumber]);
    makePaging();
    };
    return false;
    });
    }
    };

    makePaging();

    return $container;
    });

    function getInterval()
    {
    var start = start_number;
    if(current_page > last_start_page )  start = last_start_page;
    var end   = Math.min(start_number+options.block_size-1,total_page);
    if(current_page < 0 ) {
    start = 1;
    end  = 10;
    }
    return [start,end];
    };

    function startNum() {
    if((current_page%options.block_size) == 0) {
    start_number   = Math.floor((parseInt(current_page)-1)/options.block_size) * options.block_size + 1;
    }else {
    start_number   = Math.floor((parseInt(current_page))/options.block_size) * options.block_size + 1;
    }
    };

    function lastNum() {
    if((total_page%options.block_size) == 0) {
    last_start_page = Math.floor((parseInt(total_page) - 1) / options.block_size) * options.block_size + 1;
    }else {
    last_start_page = Math.floor((parseInt(total_page)) / options.block_size) * options.block_size + 1;
    }
    };

    };


    $.fn.paging.defaults = {
    'total_record' : 0 ,
    'current_page' : 1 ,
    'items_per_page':10,
    'link_class' : 'link_num',
    'selected_tag'  : 'span',
    'selected_class' : 'current' ,
    'prev_class' : 'link_num direction prev pga' ,
    'next_class' : 'link_num direction next pga' ,
    'first_text' : '&lsaquo;&lsaquo;', 'first_class' : 'link_num direction first pga',
    'last_text'  : '&rsaquo;&rsaquo;',  'last_class' : 'link_num direction last pga',
    'block_size':10 ,
    'addTag'    : "",
    'link_to'   : "#",
    'prev_text'    : "&lsaquo;",
    'next_text'    : "&rsaquo;",
    'prev_show_always' :true,
    'first_last_show' :true,
    'next_show_always' :true,
    'link_click' : false
    };

})(jQuery);
